package org.swingDemo.UI;

import com.formdev.flatlaf.FlatIntelliJLaf;
import org.swingDemo.UI.frame.ExitConfirmFrame;
import org.swingDemo.UI.panel.FolderContentPanel;
import org.swingDemo.UI.panel.FolderPathPanel;
import org.swingDemo.UI.panel.ImageOperatePanel;
import org.swingDemo.UI.panel.OpenFolderPanel;
import org.swingDemo.util.IconUtil;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.List;

public class MainFrame extends ExitConfirmFrame {

    public static MainFrame mainFrame;

    private JPanel contentPanel;
    private JTabbedPane tabbedPane;

    private FolderPathPanel folderPathPanel;
    private OpenFolderPanel openFolderPanel;
    private FolderContentPanel folderContentPanel;
    private ImageOperatePanel imageOperatePanel;

    public MainFrame() {
        super("图片分类");
        mainFrame = this;

        try {
            UIManager.setLookAndFeel(new FlatIntelliJLaf());
            initFrame();

            this.setIconImage(IconUtil.FrameIcon.getImage());
            this.setMaximumSize(new Dimension(1200, 600));
            this.setMinimumSize(new Dimension(800, 600));
            this.addWindowListener(this);
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "窗口初始化失败...");
        }
    }

    private void initFrame() {
        folderPathPanel = new FolderPathPanel();
        openFolderPanel = new OpenFolderPanel();
        folderContentPanel = new FolderContentPanel();
        imageOperatePanel = new ImageOperatePanel();

        tabbedPane = new JTabbedPane();
        tabbedPane.add("打开目录", openFolderPanel);
        tabbedPane.add("目录内容", folderContentPanel);
        tabbedPane.add("图片预览", imageOperatePanel);
        contentPanel = new JPanel(new BorderLayout());
        contentPanel.add(folderPathPanel, BorderLayout.PAGE_START);
        contentPanel.add(tabbedPane, BorderLayout.CENTER);
        this.setContentPane(contentPanel);
    }

    public void displayFolder(File folder) {
        if (folder == null) {
            return;
        }
        folderPathPanel.updateFolderPath(folder);
        tabbedPane.setSelectedComponent(folderContentPanel);
        folderContentPanel.refreshPanel(folder);
    }

    public void openParentFolder() {
        final String currentFolderPath = folderPathPanel.getFolderPath();
        if (currentFolderPath.isEmpty()) {
            return;
        }
        displayFolder((new File(currentFolderPath).getParentFile()));
    }

    public void openSubFolder(File file) {
        displayFolder(file);
    }

    public void operateImage(File file, List<File> fileList) {
        tabbedPane.setSelectedComponent(imageOperatePanel);
        imageOperatePanel.operateFile(file, fileList);
    }
}
